package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvimgDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Create by Boller on 2022/2/25 21:18
 */
@SuppressWarnings("All")
@RestController
@RequestMapping("/ydd_advimg")
public class AdvimgController {

    @Autowired
    private AdvimgService advimgService;

    @Autowired
    private AdvposService advposService;


    /**
     * 广告图管理--分页查询
     *
     * @param page
     * @param limit
     * @param advimgdesc
     * @param advimgpos
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Integer page, Integer limit, String advimgdesc, String advimgpos) {
        Page<Advimg> pageInfo = new Page<>(page, limit);

        LambdaQueryWrapper<Advimg> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.like(advimgdesc != null, Advimg::getAdvimgdesc, advimgdesc);
        queryWrapper.like(advimgpos != null, Advimg::getAdvimgpos, advimgpos);

        advimgService.page(pageInfo, queryWrapper);

        Page<AdvimgDto> advimgDtoPage = new Page<>(page, limit);

        List<AdvimgDto> advimgDtos = pageInfo.getRecords().stream().map((advimg) -> {

            AdvimgDto advimgDto = new AdvimgDto();
            BeanUtils.copyProperties(advimg, advimgDto);
            Advpos advpos = advposService.getById(advimg.getAdvimgpos());

            if (advpos != null) {
                advimgDto.setAdvposname(advpos.getAdvposname());
            }
            return advimgDto;
        }).collect(Collectors.toList());
        advimgDtoPage.setRecords(advimgDtos);
        BeanUtils.copyProperties(pageInfo, advimgDtoPage, "records");
        return ResultData.ok(PageResult.init(advimgDtoPage));
    }

    /**
     * 广告图管理--根据id查询单个数据
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        return ResultData.ok(advimgService.getById(id));
    }


    /**
     * 广告图管理--根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(Integer id) {

        advimgService.removeById(id);
        return ResultData.ok("删除成功");
    }

    /**
     * 广告图管理--修改
     *
     * @param advimg
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Advimg advimg) {

        advimgService.updateById(advimg);
        return ResultData.ok("修改成功");
    }

    /**
     * 广告图管理--添加
     *
     * @param advimg
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Advimg advimg) {
        advimgService.save(advimg);
        return ResultData.ok("添加成功");
    }
}
